package com.activity.fragment;

import android.os.Bundle;

import com.activity.OpusDetailActivity;
import com.activity.adapter.PersonalAdapter;
import com.api.param.PageParam;
import com.api.viewModel.LoginVM;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.donkingliang.groupedadapter.layoutmanger.GroupedGridLayoutManager;
import com.example.apple.ddhr_new.activity.widget.PictureEditView;

import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.apple.ddhr_new.utils.ScreenUtil;
import com.google.gson.Gson;

import event.MessageEvent;

public class OpusFragment extends BaseListViewFragment<LoginVM> {
    private PersonalAdapter personalAdapter;
    PageParam pageParam = new PageParam();

    @Override
    public void createView() {
        super.createView();

        headerFresh();

        personalAdapter=new PersonalAdapter(mContext);

        recycleView.setPadding(ScreenUtil.dp2px(mContext,3),ScreenUtil.dp2px(mContext,6),ScreenUtil.dp2px(mContext,3),0);

        recycleView.setAdapter(personalAdapter);
        recycleView.addItemDecoration(new PictureEditView.SquareLayoutItemDecoration(ScreenUtil.dp2px(mContext,6),ScreenUtil.dp2px(mContext,6),2));
        //直接使用GroupGridLayoutManager实现子项的Grid效果
        GroupedGridLayoutManager gridLayoutManager = new GroupedGridLayoutManager(mContext, 2, personalAdapter);
        recycleView.setLayoutManager(gridLayoutManager);

        personalAdapter.setOnChildClickListener(new GroupedRecyclerViewAdapter.OnChildClickListener() {
            @Override
            public void onChildClick(GroupedRecyclerViewAdapter adapter, BaseViewHolder holder, int groupPosition, int childPosition) {
                Bundle param  = new Bundle();
                Gson gson =new Gson();
                param.putString("param", gson.toJson(personalAdapter.data.get(childPosition)));

                ActivityUtil.startActivity(mContext,OpusDetailActivity.class,param);
            }
        });

        viewModel.workError.observe(this,error->{
            if(error){
                headerFreshEnd();
            }else {
                pageParam.pagenum = (Integer.valueOf(pageParam.pagenum)-1)+"";
                footerFreshEnd();
            }
        });



        viewModel.work.observe(this,data->{

            if(data.isHeader){

                personalAdapter.data.clear();
                personalAdapter.data.addAll(data.getResult());

                headerFreshEnd();
            }else
            {
                personalAdapter.data.addAll(data.getResult());
                if(data.getResult().size()>0){
                    footerFreshEnd();
                }else
                {
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
            personalAdapter.notifyDataSetChanged();

            if(personalAdapter.data.size()>0){
                showEmpty(false);
            }else {
                showEmpty(true);
            }




        });
    }

    @Override
    protected void headerFreshing() {
        pageParam.pagenum ="1";
        refreshLayout.resetNoMoreData();
        addSubscription(viewModel.getWork(pageParam,true));
    }

    @Override
    protected void footerFreshing() {
        pageParam.pagenum = (Integer.valueOf(pageParam.pagenum)+1)+"";

        addSubscription(viewModel.getWork(pageParam,false));
    }

    @Override
    public void onMessageEventBus(MessageEvent event) {
        super.onMessageEventBus(event);
        if(event.getMessage() == MessageEvent.MESSAGE_EVENT_ADD_WORKS){


            headerFreshing();

        }
    }
}
